package cn.edu.tju.wallet.controller;

import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.ResultCodeEnum;
import cn.edu.tju.wallet.domain.VirtualWallet;
import cn.edu.tju.wallet.dto.OverdraftRequestDTO;
import cn.edu.tju.wallet.dto.RepayRequestDTO;
import cn.edu.tju.wallet.service.OverdraftService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 透支控制器
 * @author 付佳腾
 */
@RestController
@RequestMapping("/api/wallet/overdraft")
@Tag(name = "透支管理")
public class OverdraftController {

    @Autowired
    private OverdraftService overdraftService;

    /**
     * 透支接口
     * @param request 透支请求参数
     * @return 操作结果
     */
    @PostMapping("/apply")
    @Operation(summary = "申请透支", description = "VIP用户可以使用透支额度进行支付")
    public HttpResult<Void> overdraft(@Valid @RequestBody OverdraftRequestDTO request) {
        try {
            // 参数验证（@Valid会自动验证，这里做额外业务验证）
            if (request.getUserId() == null || request.getUserId() <= 0) {
                return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, "用户ID不能为空");
            }
            if (request.getAmount() == null || request.getAmount().compareTo(java.math.BigDecimal.ZERO) <= 0) {
                return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, "透支金额必须大于0");
            }

            overdraftService.overdraft(request.getUserId(), request.getAmount());
            return HttpResult.success();
        } catch (IllegalArgumentException e) {
            return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, e.getMessage());
        } catch (IllegalStateException e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        } catch (RuntimeException e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, "透支失败: " + e.getMessage());
        }
    }

    /**
     * 还款接口
     * @param request 还款请求参数
     * @return 还款结果
     */
    @PostMapping("/repay")
    @Operation(summary = "还款", description = "用户偿还透支金额，系统会自动计算利息")
    public HttpResult<VirtualWallet.RepayResult> repay(@Valid @RequestBody RepayRequestDTO request) {
        try {
            // 参数验证（@Valid会自动验证，这里做额外业务验证）
            if (request.getUserId() == null || request.getUserId() <= 0) {
                return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, "用户ID不能为空");
            }
            if (request.getAmount() == null || request.getAmount().compareTo(java.math.BigDecimal.ZERO) <= 0) {
                return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, "还款金额必须大于0");
            }

            VirtualWallet.RepayResult result = overdraftService.repay(request.getUserId(), request.getAmount());
            return HttpResult.success(result);
        } catch (IllegalArgumentException e) {
            return HttpResult.failure(ResultCodeEnum.PARAM_ERROR, e.getMessage());
        } catch (IllegalStateException e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        } catch (RuntimeException e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, "还款失败: " + e.getMessage());
        }
    }
}